package com.umbrella.manage.utils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class AddressUtils {
	Logger logger = LoggerFactory.getLogger(AddressUtils.class);
	public static String GET_IP_INFO_TAOBAO_URL = "http://ip.taobao.com/service/getIpInfo.php";
	public static String GEt_IP_INFO_126_URL = "http://ip.ws.126.net/ipquery";

	/**
	 *
	 * @param content
	 *            请求的参数 格式为：name=xxx&pwd=xxx
	 * @param encoding
	 *            服务器端请求编码。如GBK,UTF-8等
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static Map<String, String> getAddresses(String ip) throws UnsupportedEncodingException {
		Map<String, String> map = new HashMap<String, String>();
		String address = "";
		//String returnStr = HttpClientService.getResult(GEt_IP_INFO_126_URL, ip, "GBK", "GET");
		String returnStr = HttpClientService.doGet(GEt_IP_INFO_126_URL+"?"+ip);
		returnStr = returnStr.substring(returnStr.indexOf("{"), returnStr.length());
		if (returnStr != null) {
			// 处理返回的省市区信息
			if (returnStr != null) {
				JSONObject ob = JSON.parseObject(returnStr);
				map.put("province", (ob.getString("province")));
				map.put("city", (ob.getString("city")));
				address = (ob.getString("province")) + "-" + (ob.getString("city"));
				map.put("address", address);
			}
			return map;
		}
		return null;
	}

}
